package com.example.yiparkapplication.tool;

import android.os.AsyncTask;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;

public class FastPay {

    private IAlPayResultListener mIAliPayResultListener = null;
    private AppCompatActivity mActivity = null;

    private FastPay(AppCompatActivity activity){
        this.mActivity = activity;
    }

    public static FastPay create(AppCompatActivity activity){
        return new FastPay(activity);
    }

    public FastPay setPayResultListener(IAlPayResultListener listener){
        this.mIAliPayResultListener = listener;
        return this;
    }

    public void alPay(String paySign){
        final PayAsyncTask payAsyncTask = new PayAsyncTask(mActivity, mIAliPayResultListener);
        payAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, paySign);
    }

    public void weChatPay(String response){
        final IWXAPI iwxapi = WindyWeChat.getInstance().getWXAPI();
        final String appId = ConfigKeys.WE_CHAT_APP_ID.toString();
        iwxapi.registerApp(appId);

        final JSONObject result = JSON.parseObject(response).getJSONObject("msg");
        final String prepayId = result.getString("prepayid");
        final String partnerId = result.getString("partnerid");
        final String packageValue = result.getString("package");
        final String nonceStr = result.getString("noncestr");
        final String timestamp = result.getString("timestamp");
        final String paySign = result.getString("sign");

        final PayReq payReq = new PayReq();
        payReq.appId = appId;
        payReq.prepayId = prepayId;
        payReq.partnerId = partnerId;
        payReq.packageValue = packageValue;
        payReq.timeStamp = timestamp;
        payReq.nonceStr = nonceStr;
        payReq.sign = paySign;
        iwxapi.sendReq(payReq);
    }
}
